package cn.edu.cqupt.cxcy.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.set.SynchronizedSet;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import cn.edu.cqupt.cxcy.dto.TeamImpl;
import cn.edu.cqupt.cxcy.model.Project;
import cn.edu.cqupt.cxcy.util.Tools;

public class TeamReleaseProject extends HttpServlet {
   @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	// TODO Auto-generated method stub
	this.doPost(req, resp);
}
   @Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    resp.setContentType("text/html;charset=utf-8");
	    HttpSession session = req.getSession();
	    List<Object> list = (List<Object>)session.getAttribute("user");
	    int te_id = (int) list.get(1);
		Project project = new Project();
		project.setTe_id(te_id);
		DiskFileItemFactory factory = new DiskFileItemFactory();
		String savePath = req.getServletContext().getRealPath("/fileOrImg");
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("utf-8");
		File f = new File(savePath);
		if(!f.exists()){
			f.mkdirs();
		}
		factory.setRepository(f);
		factory.setSizeThreshold(1024*1024*1024);
		try {
			List<FileItem> items = upload.parseRequest(req);
			for(FileItem item:items){
				if(!item.isFormField() && item != null){
					String[] names = new String[3];//字符串数组,分别为文件类型,数据库中存储的文件名,文件保存到服务器的地址
					String foot = item.getName().substring(item.getName().indexOf("."));
					String realFileName = Tools.getRandom(10)+foot;
					String dbName = item.getName().substring(item.getName().lastIndexOf("\\")+1);
					names[0] = item.getFieldName();//得到文件的名称,可以用这个参数来识别上传的文件类型
					names[1] = "fileOrImg/"+realFileName;
					names[2] = dbName;
					File dir = new File(savePath);
					File file = null;
					if(!dir.exists()){
						dir.mkdirs();
					}
					file = new File(savePath,realFileName);
					item.write(file);
					if(names[0].equals("project_image")){
						project.setP_logo(names[1]);
					}else if(names[0].equals("project_file")){
						project.setP_filepath(names[1]);
						project.setP_filename(names[2]);
					}
				}else if(item.isFormField() && item != null){
					if(item.getFieldName().equals("project_name")){
						project.setP_name(item.getString("utf-8"));
					}else if(item.getFieldName().equals("proExplain")){
						project.setP_content(item.getString("utf-8"));
					}else if(item.getFieldName().equals("project_intro")){
						project.setP_explain(item.getString("utf-8"));
					}else if(item.getFieldName().equals("project_type")){
						String type = item.getString("utf-8").trim();
						TeamImpl impl  = new TeamImpl();
						int t_type = impl.getTypeByName(type);
						project.setPt_id(t_type);
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}catch(Exception e1){
			e1.printStackTrace();
		}
		TeamImpl teamImpl = new TeamImpl();
		teamImpl.insertProjectInfo(project);
		
		String pjax_back = "$(function(){$(\"a.turn_pjax[href='team/pubpro']\").trigger('click');});";
		req.setAttribute("pjax_back", pjax_back);
		req.getRequestDispatcher("Tteam.jsp").forward(req, resp);
//		req.getRequestDispatcher("Tproj.jsp").forward(req, resp);
	}
}
